//
// Created by andy on 2018/3/10.
//

#ifndef COMMUNICATION_LOG_H
#define COMMUNICATION_LOG_H

#include <pthread.h>
#include <string>
namespace yfxt
{
    class Logger
    {
    public:
        enum LogLevel {
            INVALID = -1,
            INFO,
            DEBUG,
            WARNING,
            ERROR,
            TRACE
        };
    public:
        static Logger * instance();
        void initialize(const char * path, int traceDepth, bool serialize);
        void close();
        void log(LogLevel level, const char * msg);
        void log(LogLevel level, const char * file, const char * fun, int line, const char* msg, bool trace = false);
        virtual ~Logger();
    private:
        void logout(const char * msg);
        void logformat(LogLevel level, const char * msg);
    private:
        static Logger * sInstance;
        Logger();
        std::string mFileName;
        char * mCache;
        int mCacheOffset;
        FILE * mOutputFile;

        bool mHasInitialized;
        bool mIsSerialize;
        int mTraceDepth;
        pthread_mutex_t mMutex;

        const static int MAX_CACHE_SIZE = 1024 * 1024;
        const static int MAX_TRACE_DEPTH = 10;
    };



}
#define LOG_INFO(x) yfxt::Logger::instance()->log(yfxt::Logger::INFO, x)
#define LOG_DEBUG(x) yfxt::Logger::instance()->log(yfxt::Logger::DEBUG, __FILE__, __FUNCTION__, __LINE__, x)
#define LOG_WARNING(x) yfxt::Logger::instance()->log(yfxt::Logger::WARNING, x)
#define LOG_ERROR(x) yfxt::Logger::instance()->log(yfxt::Logger::ERROR, __FILE__, __FUNCTION__, __LINE__, x, true)
#define LOG_TRACE(x) yfxt::Logger::instance()->log(yfxt::Logger::TRACE, __FILE__, __FUNCTION__, __LINE__, x, true)
#endif //COMMUNICATION_LOG_H

